#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>

#define SERVER_PORT 8888

int main(int argc, char **argv)
{
	int sockfd;
	struct sockaddr_in server_addr;
	
	int ret;
	unsigned char SendBuf[1000];
	int Send_Len;

	if (argc != 2)
	{
		printf("Usage:\n");
		printf("%s <server_ip>\n", argv[0]);
		return -1;
	}

	sockfd = socket(AF_INET, SOCK_STREAM, 0);

	server_addr.sin_family      = AF_INET;
	server_addr.sin_port        = htons(SERVER_PORT);  /* host to net, short */
 	//client_addr.sin_addr.s_addr = INADDR_ANY;
 	if (0 == inet_aton(argv[1], &server_addr.sin_addr))
 	{
		printf("invalid server_ip\n");
		return -1;
	}
	memset(server_addr.sin_zero, 0, 8);


	ret = connect(sockfd, (const struct sockaddr *)&server_addr, sizeof(struct sockaddr));	
	if (-1 == ret)
	{
		printf("connect error!\n");
		return -1;
	}

	while (1)
	{
        memset(SendBuf,0,sizeof(SendBuf));

		if (fgets(SendBuf, 999, stdin))
		{
			Send_Len = send(sockfd, SendBuf, strlen(SendBuf), 0);
			if (Send_Len <= 0)
			{
				close(sockfd);
				return -1;
			}
		}
        if(!strncmp(SendBuf,"quit",4))
        {
            break;
        }
	}
	
    close(sockfd);
	return 0;
}